package com.jjwxc.jwjskandriod.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.activity.BuyBookDetailsActivity;
import com.jjwxc.jwjskandriod.adapter.PurchaseViewHolder;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFActivity;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.imageloader.FFImageLoader;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.model.BookPurchaseResponse;
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration;

import java.util.ArrayList;
import java.util.List;

public class PurchaseBookFragment extends Fragment {
    private String value;
    private String key;
    private View view;
    private RecyclerView rv_space;
    private PurchaseBookAdapter adapter;


    private List<String> mList = new ArrayList<>();

    public PurchaseBookFragment(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_purchaseinfo, container, false);
            setViewId();
            setListener();
        }
        return view;
    }

    public void setViewId() {
        rv_space = view.findViewById(R.id.rv_rechargeinfo);
        rv_space.setNestedScrollingEnabled(false);
        rv_space.setFocusable(false);
        rv_space.setLayoutManager(new LinearLayoutManager(getContext()));
        rv_space.addItemDecoration(new LinearLayouDecoration(getContext(),
                LinearLayouDecoration.HORIZONTAL, R.dimen.border8, R.color.transparent));

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void setListener() {

        rv_space.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        adapter = new PurchaseBookAdapter(getActivity());
        rv_space.setAdapter(adapter);


        afterCreate();
    }


    public void afterCreate() {
        ((FFActivity) getActivity()).post(Url.listUserBookSubscribe, null, BookPurchaseResponse.class, new FFNetWorkCallBack<BookPurchaseResponse>() {
            @Override
            public void onSuccess(BookPurchaseResponse response) {
                if (response.getCode() == 200) {
                    adapter.setData(response.getData());
                }
            }

            @Override
            public boolean onFail(BookPurchaseResponse response) {
                return false;
            }
        }, "{\"offset\":0,\"limit\":10}");


    }


    public class PurchaseBookAdapter extends BaseRecyclerAdapter<PurchaseViewHolder> {
        private Context context;


        private List<BookPurchaseResponse.DataBean> mData = new ArrayList<>();


        public PurchaseBookAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<BookPurchaseResponse.DataBean> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<BookPurchaseResponse.DataBean> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public PurchaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.purchasebook_item, parent, false);
            PurchaseViewHolder viewHolder_text = new PurchaseViewHolder(mView);
            viewHolder_text.setIsRecyclable(false);
            return viewHolder_text;

        }

        @Override
        public PurchaseViewHolder getViewHolder(View view) {
            return new PurchaseViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
        @Override
        public void onBindViewHolder(final PurchaseViewHolder holder, @SuppressLint("RecyclerView") final int position, boolean isItem) {

            FFImageLoader.loadImage(context, holder.img_purchase_item, mData.get(position).getBook().getCoverImgUrl() + "", mData.get(position).getBook().getBookId(), true);
            holder.tv_bookname_purchase.setText(mData.get(position).getBook().getName());
            holder.tv_book_purchase.setText("共" + mData.get(position).getSubscribeChapterCount() + "章节");
//            holder.tv_book_info.setText(mData.get(position).getBookIntro());

            holder.cl_purchase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), BuyBookDetailsActivity.class);
                    intent.putExtra("BOOK_NAME", mData.get(position).getBook().getName());
                    intent.putExtra("BOOK_ID", mData.get(position).getBook().getBookId());
                    startActivity(intent);

                }
            });


        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }


    }

}